
package com.qdu.dao;

import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author 联想
 */
//1.用户登录
public class UserLoginDao {

    private static int SUCCESS = 1;
    private static int ERROR = 2;
    private static int SAME = 3;

    public int userLogin(String member_loginname, String member_password) {
        Connection con = null;//接收数据库数据
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from commonmember where member_loginname=? and member_password=? ");       
            ps.setString(1, member_loginname);//参数表示问号
            ps.setString(2, member_password);
            rs = ps.executeQuery();
            if (rs.next()) { // 执行成功
                return this.SUCCESS;
            } else { // 执行失败
                return this.ERROR;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }
//2.获取用户对应的级别

    public String getJiBie(String member_loginname) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String str = null;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select member_level from commonmember where member_loginname=?");
            ps.setString(1, member_loginname);
            rs = ps.executeQuery();
            rs.next();
            float f = rs.getFloat(1);
            if (f < 1000) {
                str = "普通";
            } else if (f > 1000 && f < 2000) {
                str = "银卡";
            } else if (f > 2000) {
                str = "金卡";
            }
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return "普通";
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }
//3.新用户注册 
public int regUser(String member_loginname, String member_password, String member_name, String member_sex, String member_email, String member_tel) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Random random = new Random();
        int r = random.nextInt(1000);
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from commonmember where member_loginname=?");
            ps.setString(1, member_loginname);            
            rs = ps.executeQuery();
            con.commit();//数据库缓存问题
            if (rs.next()) { // 有重名
                return this.SAME;
            } else { // 无重名

                ps = con.prepareStatement("insert into commonmember(member_id,member_loginname,member_password,member_sex,member_truename,member_email,member_tel,member_level) values(?,?,?,?,?,?,?,?)");
                ps.setInt(1, r);
                ps.setString(2, member_loginname);
                ps.setString(3, member_password);
                ps.setString(4, member_sex);
                ps.setString(5, member_name);
                ps.setString(6, member_email);
                ps.setString(7, member_tel);
                ps.setFloat(8, 0);

                int i = ps.executeUpdate();
                if (i == 1) { // 执行成功
                    return this.SUCCESS;
                } else { // 执行失败
                    return this.ERROR;

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    }

//4.通过"用户名称"和"电子邮件"找回普通用户的密码
    public int getMiMa(String member_loginname, String member_email) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from commonmember where member_loginname=? and member_email=?");
             ps.setString(1, member_loginname);
             ps.setString(2, member_email);
            rs = ps.executeQuery();
            if (rs.next()) {
                ps = con.prepareStatement("update commonmember set member_password='123456' where member_loginname=?");
                ps.setString(1, member_loginname);
                int i = ps.executeUpdate();                
                if (i == 1) {
                    return this.SUCCESS;
                } else {
                    return this.ERROR;
                }
            } 
            else {
                return this.SAME;//此处指密码错误
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }

    }
//5.修改时获取用户信息
    public List getUser(String member_loginname) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from commonmember where member_loginname=?");
            ps.setString(1, member_loginname);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(rs.getString(1));
                list.add(rs.getString(2));
                list.add(rs.getString(3));
                list.add(rs.getString(4));
                list.add(rs.getString(5));
                list.add(rs.getString(6));
                list.add(rs.getString(7));

                list.add(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;

    }
//6.修改指定用户名称的普通用户信息
    public int upUser(String member_loginname, String member_password, String member_truename, String member_sex, String member_email, String member_tel) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("update commonmember set member_password=?, member_truename=?,member_sex=?,member_email=?,member_tel=? where member_loginname=?" );
            ps.setString(1, member_password);
            ps.setString(2, member_truename);
            ps.setString(3, member_sex);
            ps.setString(4, member_email);
            ps.setString(5, member_tel);
            ps.setString(6, member_loginname);

            int i = ps.executeUpdate();
            if (i == 1) {
                return this.SUCCESS;
            } else {
                return this.ERROR;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.ERROR;
        } finally {
            DatabaseUtil.close(null, ps, con);
        }

    }

}
